<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\SCloudLessonInfo;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CloudLessonController extends Controller {
	use HasResourceActions;

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function index(Content $content) {
		return $content
			->header('云课堂')
			->description('列表')
			->body($this->grid());
	}

	/**
	 * Show interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function show($id, Content $content) {
		return $content
			->header('云课堂')
			->description('详情')
			->body($this->detail($id));
	}

	/**
	 * Edit interface.
	 *
	 * @param mixed $id
	 * @param Content $content
	 * @return Content
	 */
	public function edit($id, Content $content) {
		return $content
			->header('云课堂')
			->description('编辑')
			->body($this->form()->edit($id));
	}

	/**
	 * Create interface.
	 *
	 * @param Content $content
	 * @return Content
	 */
	public function create(Content $content) {
		return $content
			->header('云课堂')
			->description('新增')
			->body($this->form());
	}

	/**
	 * Make a grid builder.
	 *
	 * @return Grid
	 */
	protected function grid() {
		$grid = new Grid(new SCloudLessonInfo);

		$grid->id('ID')->sortable();
		$grid->title('云课堂标题');
		$grid->use_type('使用对象')->using(config("svs.use_type"));
		$grid->course_type('课程类型')->using(config("svs.course_type"));
		$grid->tags('标签');
		$grid->desc('简介');
		$grid->lesson_time('课程时间');
		$grid->base_num('基础观看人数');
		$grid->look_num('实际观看人数');
		$grid->require_share_num('要求分享注册次数');
		$grid->share_num('被分享次数');
		$grid->poster('封面')->image();
		// $grid->res_url('资源地址');
		$grid->original_price('原价')->display(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		$grid->actual_price('现价')->display(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		// $grid->is_free('是否免费');
		$states = [
			'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
			'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
		];
		$grid->is_free("免费")->switch($states);

		$states = [
			'on' => ['value' => 1, 'text' => '上架', 'color' => 'success'],
			'off' => ['value' => 0, 'text' => '下降', 'color' => 'danger'],
		];
		$grid->on_sale("状态")->switch($states);

		// $grid->is_recommend('是否推荐')->using( config('svs.boolean') );

		$grid->created_at('创建时间')->sortable();
		$grid->updated_at('更新时间');

		//禁用导出
		$grid->disableExport();
		return $grid;
	}

	/**
	 * Make a show builder.
	 *
	 * @param mixed $id
	 * @return Show
	 */
	protected function detail($id) {
		$show = new Show(SCloudLessonInfo::findOrFail($id));

		$show->id('Id');
		$show->title('云课堂标题');

		$show->use_type('使用对象')->as(function ($p) {
			return config("svs.use_type." . $p);
		});
		$show->use_type('课程类型')->as(function ($p) {
			return config("svs.course_type." . $p);
		});

		$show->tags('标签');
		$show->desc('简介');
		$show->lesson_time('课程时间');
		$show->base_num('基础观看人数');
		$show->look_num('实际观看人数');
		$show->require_share_num('要求分享注册次数');
		$show->share_num('分享次数');
		$show->res_url('资源地址')->file();
		$show->poster('封面')->image();
		$show->original_price('原价')->as(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		$show->actual_price('现价')->as(function ($p) {
			return sprintf("%.2f", $p / 100);
		});
		$show->is_free('是否免费')->using(config('svs.boolean'));
		$show->is_free('是否上架')->using(config('svs.boolean'));
		// $show->is_recommend('是否推荐')->using( config('svs.boolean') );
		// $show->admin_id('Admin id');
		$show->created_at('创建时间');
		$show->updated_at('更新时间');
		// $show->deleted_at('Deleted at');

		return $show;
	}

	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form() {
		$form = new Form(new SCloudLessonInfo);

		$form->text('title', '云课堂标题')->rules('required');

		$form->select('use_type', '使用对象')->options(config('svs.use_type'));
		$form->select('course_type', '课程类型')->options(config('svs.course_type'));

		$form->text('tags', '标签')->help("多个标签用英文逗号隔开")->rules('required|min:2');
		$form->textarea('desc', '简介')->rows(8)->rules('required');

		$form->datetime('lesson_time', '课程时间')->format('YYYY-MM-DD HH:00:00')->rules('required');

		$form->number('base_num', '基础观看人数')->min(0);
		// $form->number('look_num','实际观看人数');
		$form->number('require_share_num', '要求分享注册次数')->help("注册次数达到该数值时可观看视频")->rules("min:0");
		// $form->number('share_num','被分享次数');

		$form->image('poster', "封面");
		$form->file('res_url', '上传文件')->rules('required');
		// 异步上传 https://learnku.com/articles/18159
		$form->number('original_price', '原价（单位分）')->min(0);
		$form->number('actual_price', '现价（单位分）')->min(0);
		// $form->number('is_free','是否免费');

		$states = [
			'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
			'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
		];

		$form->switch("is_free", "是否免费")->states($states);

		$states = [
			'on' => ['value' => 1, 'text' => '上架', 'color' => 'success'],
			'off' => ['value' => 0, 'text' => '下降', 'color' => 'danger'],
		];
		$form->switch("on_sale", "状态")->states($states);

		$form->hidden('admin_id')->value(\Admin::user()->id);

		$form->footer(function ($footer) {

			// 去掉`查看`checkbox
			$footer->disableViewCheck();

			// 去掉`继续编辑`checkbox
			$footer->disableEditingCheck();

			// 去掉`继续创建`checkbox
			$footer->disableCreatingCheck();

		});

		return $form;
	}
}
